#!/bin/bash

echo "=========================================="
echo "  主机巡检邮件功能配置向导"
echo "=========================================="
echo ""

# 设置用户邮箱
echo "📧 步骤1：设置用户邮箱地址"
echo "请输入要设置邮箱的用户名（默认: admin）:"
read -p "用户名: " USERNAME
USERNAME=${USERNAME:-admin}

echo "请输入邮箱地址（例如: 15921733150@163.com）:"
read -p "邮箱: " EMAIL

if [ -z "$EMAIL" ]; then
    echo "❌ 邮箱地址不能为空！"
    exit 1
fi

cd /aiops/backend
python manage.py shell << EOF
from django.contrib.auth.models import User
try:
    user = User.objects.get(username='$USERNAME')
    user.email = '$EMAIL'
    user.save()
    print(f"\\n✅ 已设置 {user.username} 的邮箱为: {user.email}")
except User.DoesNotExist:
    print(f"\\n❌ 用户 '$USERNAME' 不存在！")
EOF

echo ""
echo "=========================================="
echo "  📝 SMTP配置说明"
echo "=========================================="
echo ""
echo "⚠️  重要：您需要手动配置SMTP设置"
echo ""
echo "1. 编辑配置文件："
echo "   nano /aiops/backend/backend/settings.py"
echo ""
echo "2. 修改以下内容："
echo "   EMAIL_HOST_USER = '你的发件邮箱@163.com'"
echo "   EMAIL_HOST_PASSWORD = '你的SMTP授权码'"
echo ""
echo "3. 获取163邮箱SMTP授权码："
echo "   - 登录163邮箱"
echo "   - 设置 → POP3/SMTP/IMAP"
echo "   - 开启SMTP服务"
echo "   - 获取授权密码（不是登录密码！）"
echo ""
echo "4. 重启后端服务使配置生效"
echo ""
echo "=========================================="
echo "  ✅ 邮箱地址配置完成！"
echo "=========================================="
echo ""
echo "接下来："
echo "1. 配置SMTP设置（见上方说明）"
echo "2. 重启后端: cd /aiops && ./restart_backend.sh"
echo "3. 访问系统: http://localhost:8080/"
echo "4. 执行巡检 → 点击发送邮件"
echo ""
